package cn.chiship.framework.third.biz.wxpub.controller;


import cn.chiship.framework.common.annotation.SystemOptionAnnotation;
import cn.chiship.framework.common.enums.BusinessTypeEnum;
import cn.chiship.framework.third.biz.wxpub.pojo.dto.BasicMaterialsDto;
import cn.chiship.framework.third.core.common.ThirdUtil;
import cn.chiship.framework.third.core.common.WechatUtils;
import cn.chiship.sdk.core.annotation.NoParamsSign;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.framework.base.BaseService;
import cn.chiship.sdk.framework.base.BaseController;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.annotation.Authorization;

import javax.annotation.Resource;

import cn.chiship.framework.third.biz.wxpub.service.ThirdWechatArticleService;
import cn.chiship.framework.third.biz.wxpub.entity.ThirdWechatArticle;
import cn.chiship.framework.third.biz.wxpub.entity.ThirdWechatArticleExample;
import cn.chiship.framework.third.biz.wxpub.pojo.dto.ThirdWechatArticleDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * 文章素材控制层
 * 2022/7/13
 *
 * @author lijian
 */
@RestController
@Authorization
@RequestMapping("/thirdWechatArticle")
@Api(tags = "文章素材")
public class ThirdWechatArticleController extends BaseController<ThirdWechatArticle, ThirdWechatArticleExample> {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThirdWechatArticleController.class);

    @Resource
    private ThirdWechatArticleService thirdWechatArticleService;

    @Override
    public BaseService getService() {
        return thirdWechatArticleService;
    }

    @SystemOptionAnnotation(describe = "文章素材分页")
    @ApiOperation(value = "文章素材分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序方式", defaultValue = "-gmtModified", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "resourceType", value = "类型", required = true, dataTypeClass = Byte.class, paramType = "query"),

    })
    @GetMapping(value = "/page")
    public ResponseEntity<BaseResult> page(@RequestParam(required = false, defaultValue = "", value = "keyword") String keyword,
                                           @RequestParam(value = "resourceType") Byte resourceType) {
        ThirdWechatArticleExample thirdWechatArticleExample = new ThirdWechatArticleExample();
        /**
         * 创造条件
         */
        ThirdWechatArticleExample.Criteria thirdWechatArticleCriteria = thirdWechatArticleExample.createCriteria();
        thirdWechatArticleCriteria.andIsDeletedEqualTo(BaseConstants.NO).andResourceTypeEqualTo(resourceType).andAppIdEqualTo(ThirdUtil.getAppId());
        if (!StringUtil.isNullOrEmpty(keyword)) {
            thirdWechatArticleCriteria.andTitleLike("%" + keyword + "%");
        }
        return super.responseEntity(BaseResult.ok(super.page(thirdWechatArticleExample)));
    }

    @SystemOptionAnnotation(describe = "文章素材列表数据")
    @ApiOperation(value = "文章素材列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "resourceType", value = "类型", required = true, dataTypeClass = Byte.class, paramType = "query"),
    })
    @GetMapping(value = "/list")
    public ResponseEntity<BaseResult> list(@RequestParam(required = false, defaultValue = "", value = "keyword") String keyword,
                                           @RequestParam(value = "resourceType") Byte resourceType) {
        ThirdWechatArticleExample thirdWechatArticleExample = new ThirdWechatArticleExample();
        /**
         * 创造条件
         */
        ThirdWechatArticleExample.Criteria thirdWechatArticleCriteria = thirdWechatArticleExample.createCriteria();
        thirdWechatArticleCriteria.andIsDeletedEqualTo(BaseConstants.NO).andResourceTypeEqualTo(resourceType).andAppIdEqualTo(ThirdUtil.getAppId());
        if (!StringUtil.isNullOrEmpty(keyword)) {
            thirdWechatArticleCriteria.andTitleLike("%" + keyword + "%");
        }
        return super.responseEntity(BaseResult.ok(super.list(thirdWechatArticleExample)));
    }

    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_SAVE, describe = "保存文章素材")
    @ApiOperation(value = "保存文章素材")
    @PostMapping(value = "save")
    public ResponseEntity<BaseResult> save(@RequestBody @Valid ThirdWechatArticleDto thirdWechatArticleDto) {
        ThirdWechatArticle thirdWechatArticle = new ThirdWechatArticle();
        BeanUtils.copyProperties(thirdWechatArticleDto, thirdWechatArticle);
        return super.responseEntity(super.save(thirdWechatArticle));
    }

    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_SAVE, describe = "保存基本素材(文本、视频、音频、图片)")
    @ApiOperation(value = "保存基本素材")
    @PostMapping(value = "saveBasic")
    @NoParamsSign
    public ResponseEntity<BaseResult> saveBasic(@Valid BasicMaterialsDto basicMaterialsDto, @RequestParam(required = false, value = "file") MultipartFile file) {
        return super.responseEntity(thirdWechatArticleService.saveBasic(basicMaterialsDto, file, getLoginUser()));
    }


    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_UPDATE, describe = "更新文章素材")
    @ApiOperation(value = "更新文章素材")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataTypeClass = String.class, paramType = "path", required = true)
    })
    @PostMapping(value = "update/{id}")
    public ResponseEntity<BaseResult> update(@PathVariable("id") String id, @RequestBody @Valid ThirdWechatArticleDto thirdWechatArticleDto) {
        ThirdWechatArticle thirdWechatArticle = new ThirdWechatArticle();
        BeanUtils.copyProperties(thirdWechatArticleDto, thirdWechatArticle);
        return super.responseEntity(super.update(id, thirdWechatArticle));
    }

    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_REMOVE, describe = "删除文章素材")
    @ApiOperation(value = "删除文章素材")
    @PostMapping(value = "remove")
    public ResponseEntity<BaseResult> remove(@RequestBody(required = true) List<String> ids) {
        ThirdWechatArticleExample thirdWechatArticleExample = new ThirdWechatArticleExample();
        thirdWechatArticleExample.createCriteria().andIdIn(ids);
        return super.responseEntity(super.remove(thirdWechatArticleExample));
    }
}